package com.share.lottery.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.share.lottery.R;
import com.share.lottery.base.BaseApplication;
import com.share.lottery.model.User;

public class AppUtils {
	
	public static boolean isNetAvaliable(Context context) {
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(context.CONNECTIVITY_SERVICE);
		if (connectivity == null) {
			Log.i("NetWorkState", "Unavailable");
			return false;
		} else {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						Log.i("NetWorkState", "avaliable");
						return true;
					}
				}
			}
		}

		return false;
	}
	// 序列化userbean到本地
		public static void saveUserBean(User userbean) {
			SharedPreferences mSharedPreferences = BaseApplication.getInstance()
					.getSharedPreferences("base64", Context.MODE_PRIVATE);
			try {
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				ObjectOutputStream oos = new ObjectOutputStream(baos);
				oos.writeObject(userbean);

				String personBase64 = new String(Base64.encode(baos.toByteArray(),
						Base64.DEFAULT));
				SharedPreferences.Editor editor = mSharedPreferences.edit();
				editor.putString("person", personBase64);
				editor.commit();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		public static User getUserBean() {
			try {
				SharedPreferences mSharedPreferences = BaseApplication
						.getInstance().getSharedPreferences("base64",
								Context.MODE_PRIVATE);
				String personBase64 = mSharedPreferences.getString("person", "");
				byte[] base64Bytes = Base64.decode(personBase64, Base64.DEFAULT);// Base64.decodeBase64(personBase64.getBytes(),Base64.DEFAULT);
				ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
				ObjectInputStream ois = new ObjectInputStream(bais);
				User user = (User) ois.readObject();

				return user;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}

		}
		
		public static void clearBean() {
			try {
				SharedPreferences mSharedPreferences = BaseApplication
						.getInstance().getSharedPreferences("base64",
								Context.MODE_PRIVATE);
				SharedPreferences.Editor editor = mSharedPreferences.edit();
				editor.clear();
				editor.commit();
				
			} catch (Exception e) {
				e.printStackTrace();
				
			}

		}
		
	

		
//		// 序列化userbean到本地
//				public static void saveYlUserBean(YlUser userbean) {
//					SharedPreferences mSharedPreferences = BaseApplication.getInstance()
//							.getSharedPreferences("base64", Context.MODE_PRIVATE);
//					try {
//						ByteArrayOutputStream baos = new ByteArrayOutputStream();
//						ObjectOutputStream oos = new ObjectOutputStream(baos);
//						oos.writeObject(userbean);
//
//						String personBase64 = new String(Base64.encode(baos.toByteArray(),
//								Base64.DEFAULT));
//						SharedPreferences.Editor editor = mSharedPreferences.edit();
//						editor.putString("ylperson", personBase64);
//						editor.commit();
//					} catch (IOException e) {
//						e.printStackTrace();
//					}
//				}
//
//				public static YlUser getYlUserBean() {
//					try {
//						SharedPreferences mSharedPreferences = BaseApplication
//								.getInstance().getSharedPreferences("base64",
//										Context.MODE_PRIVATE);
//						String personBase64 = mSharedPreferences.getString("ylperson", "");
//						byte[] base64Bytes = Base64.decode(personBase64, Base64.DEFAULT);// Base64.decodeBase64(personBase64.getBytes(),Base64.DEFAULT);
//						ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
//						ObjectInputStream ois = new ObjectInputStream(bais);
//						YlUser user = (YlUser) ois.readObject();
//
//						return user;
//					} catch (Exception e) {
//						e.printStackTrace();
//						return null;
//					}
//
//				}
//				

}
